{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/145_chart_image.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Image Charts**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import calendar\n",
    "import ee\n",
    "import geemap\n",
    "from geemap import chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_by_region"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "image = (\n",
    "    ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands().select(\"[0-9][0-9]_tmean\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = calendar.month_abbr[1:]  # a list of month labels, e.g. ['Jan', 'Feb', ...]\n",
    "title = \"Average Monthly Temperature by Ecoregion\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_by_region(\n",
    "    image,\n",
    "    ecoregions,\n",
    "    reducer=\"mean\",\n",
    "    scale=500,\n",
    "    x_property=\"label\",\n",
    "    title=title,\n",
    "    x_label=\"Ecoregion\",\n",
    "    y_label=\"Temperature\",\n",
    "    labels=labels,\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/y4rp3dK.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_regions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "image = (\n",
    "    ee.ImageCollection(\"OREGONSTATE/PRISM/Norm91m\").toBands().select(\"[0-9][0-9]_ppt\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = [str(x).zfill(2) + \"_ppt\" for x in range(1, 13)]\n",
    "values = calendar.month_abbr[1:]  # a list of month labels, e.g. ['Jan', 'Feb', ...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_properties = dict(zip(keys, values))\n",
    "title = \"Average Ecoregion Precipitation by Month\"\n",
    "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_regions(\n",
    "    image,\n",
    "    ecoregions,\n",
    "    reducer=\"mean\",\n",
    "    scale=500,\n",
    "    series_property=\"label\",\n",
    "    x_labels=x_properties,\n",
    "    title=title,\n",
    "    colors=colors,\n",
    "    x_label=\"Month\",\n",
    "    y_label=\"Precipitation (mm)\",\n",
    "    legend_location=\"top-left\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/5WJVCNY.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_by_class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "\n",
    "image = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD09A1\")\n",
    "    .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n",
    "    .select(\"sur_refl_b0[0-7]\")\n",
    "    .mean()\n",
    "    .select([2, 3, 0, 1, 4, 5, 6])\n",
    ")\n",
    "\n",
    "wavelengths = [469, 555, 655, 858, 1240, 1640, 2130]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_by_class(\n",
    "    image,\n",
    "    class_band=\"label\",\n",
    "    region=ecoregions,\n",
    "    reducer=\"MEAN\",\n",
    "    scale=500,\n",
    "    x_labels=wavelengths,\n",
    "    title=\"Ecoregion Spectral Signatures\",\n",
    "    x_label=\"Wavelength (nm)\",\n",
    "    y_label=\"Reflectance (x1e4)\",\n",
    "    colors=[\"#f0af07\", \"#0f8755\", \"#76b349\"],\n",
    "    legend_location=\"top-left\",\n",
    "    interpolation=\"basis\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/XqYHvBV.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD09A1\")\n",
    "    .filter(ee.Filter.date(\"2018-06-01\", \"2018-09-01\"))\n",
    "    .select([\"sur_refl_b01\", \"sur_refl_b02\", \"sur_refl_b06\"])\n",
    "    .mean()\n",
    ")\n",
    "\n",
    "region = ee.Geometry.Rectangle([-112.60, 40.60, -111.18, 41.22])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_histogram(\n",
    "    image,\n",
    "    region,\n",
    "    scale=500,\n",
    "    max_buckets=200,\n",
    "    min_bucket_width=1.0,\n",
    "    max_raw=1000,\n",
    "    max_pixels=int(1e6),\n",
    "    title=\"MODIS SR Reflectance Histogram\",\n",
    "    labels=[\"Red\", \"NIR\", \"SWIR\"],\n",
    "    colors=[\"#cf513e\", \"#1d6b99\", \"#f0af07\"],\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/mY4yoYH.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
